package com.xuxin.pda.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.xuxin.pda.R;
import com.xuxin.pda.base.BaseActivity;
import com.xuxin.pda.bean.BaseBean;
import com.xuxin.pda.bean.CommitBean;
import com.xuxin.pda.contract.TrashResultContract;
import com.xuxin.pda.popup.ConfirmPopup;
import com.xuxin.pda.presenter.TrashResultPresenter;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2021/4/27 0027
 * 医废收集信息
 */
public class TrashResultActivity extends BaseActivity implements TrashResultContract.View {
    private final TrashResultContract.Presenter presenter = new TrashResultPresenter(this);
    public static String RESULT = "RESULT";
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.button_delete)
    Button button_delete;
    @BindView(R.id.trash_result_package)
    TextView trash_result_package;
    @BindView(R.id.trash_result_medical)
    TextView trash_result_medical;
    @BindView(R.id.trash_result_weight)
    TextView trash_result_weight;
    @BindView(R.id.trash_result_dept)
    TextView trash_result_dept;
    @BindView(R.id.trash_result_doctor)
    TextView trash_result_doctor;
    @BindView(R.id.trash_result_time)
    TextView trash_result_time;

    private String trashId;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_trash_result);
    }

    @Override
    public void initView() {
        title_name.setText("医废收集信息");
        button.setText("完成");
        button_delete.setText("删除这条医废");
        title_right.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initData() {
        if (null != getIntent().getExtras()) {
            try {
                CommitBean commitBean = (CommitBean) getIntent().getExtras().getSerializable(RESULT);
                trashId = commitBean.getResult().getId();
                trash_result_package.setText(String.format("编号：%s", commitBean.getResult().getPackageCode()));
                trash_result_medical.setText(String.format("类型：%s", commitBean.getResult().getMedicalLittleClass()));
                trash_result_weight.setText(String.format("重量：%skg", commitBean.getResult().getWeight_Kg()));
                trash_result_dept.setText(String.format("来源：%s", commitBean.getResult().getDept_Name()));
                trash_result_doctor.setText(String.format("交接：%s", commitBean.getResult().getDoctorId()));
                trash_result_time.setText(String.format("时间：%s", commitBean.getResult().getCreationTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.title_left, R.id.button, R.id.button_delete})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.button) {
            finish();
        } else if (id == R.id.button_delete) {
            if (!StringUtils.isEmpty(trashId)) {
                new XPopup.Builder(context)
                        .dismissOnTouchOutside(true)
                        .dismissOnBackPressed(true)
                        .asCustom(new ConfirmPopup(context, "确定要删除这条医废记录吗？", new ConfirmPopup.OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                popup.setTitle("正在删除中...").show();
                                handler.sendEmptyMessageDelayed(0, 500);
                            }
                        })).show();
            } else {
                ToastUtils.showShort("医废信息错误，暂时无法删除");
            }

        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.deleteTrash(trashId);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(BaseBean baseBean) {
        ToastUtils.showShort("删除成功");
        finish();
    }
}